package dictcn.com.activity;

import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.RelativeLayout;
import dictcn.com.R;
import dictcn.com.model.ImageAdapter;

public class WelcomActivity extends ActivityGroup {//显示下方四个按钮，并设置相应的点击事件
	
	private GridView gridToolbar;
	private ImageAdapter imgAdapter=null;
	private RelativeLayout content=null;
	
	//四张地底部要显示的图面
	private int toolImg[] =new int[]{R.drawable.radio_dictionary_n,R.drawable.radio_memo_n,
			R.drawable.radio_translation_n,R.drawable.radio_set_n};
						
	private int width=0;
	private int height=0;
	
	private Intent intent=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.setContentView(R.layout.menu);
		
		gridToolbar=(GridView)findViewById(R.id.gridView_btn);
		content=(RelativeLayout)findViewById(R.id.layout_content);
		gridToolbar.setNumColumns(toolImg.length);
		
	
		gridToolbar.setSelector(new ColorDrawable(Color.TRANSPARENT));
		gridToolbar.setGravity(Gravity.CENTER);
		gridToolbar.setVerticalSpacing(0);
		gridToolbar.setBackgroundColor(Color.DKGRAY);
		
		width=getWindowManager().getDefaultDisplay().getWidth()/toolImg.length;
		
		Drawable da=this.getResources().getDrawable(R.drawable.radio_dictionary_n);
		height = da.getIntrinsicHeight()*5/4;
		imgAdapter=new ImageAdapter(this, toolImg, width, height, R.drawable.bottomtab_select);
		gridToolbar.setAdapter(imgAdapter);
		switchActivity(0);
		gridToolbar.setOnItemClickListener(new OnItemClickListener() {
			
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				switchActivity(arg2);
			}
		});
	}
	
	//通过点击的下方的图片。这只相应的跳转
	public void switchActivity(int id){
		imgAdapter.setFocus(id);
		content.removeAllViews();
		switch(id){
		case 0:
			intent=new Intent(WelcomActivity.this, MainInterface.class);
			break;
		case 1:
			intent=new Intent(WelcomActivity.this,WordList.class);
			break;
		case 2:
			intent=new Intent(WelcomActivity.this, Translate.class);
			break;
		case 3:
			intent=new Intent(WelcomActivity.this,Settings.class);
			break;
		}
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Window subActivity=getLocalActivityManager().startActivity("subActivity", intent);
		content.addView(subActivity.getDecorView(),
				LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);		
	}

}
